<?php
/**
 * Created by XM·Tan.
 * Motto: Who shuts love out, in turn shall be shut out from love.
 * Date: 2021/3/22 22:25
 */

namespace app\common\model\mysql;


use think\Model;

class BaseModel extends Model {

    /**
     * 自动生成写入时间戳
     * @var bool
     */
    protected $autoWriteTimestamp = true;

    /**
     * 列表页获取数据
     * @param $likeKeys
     * @param $data
     * @param $limit
     * @param array $order
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getLists($likeKeys, $data, $limit, $order = ['id' => 'ASC']) {
        if (!empty($likeKeys)) {
            // 搜索器
            $res = $this->withSearch($likeKeys, $data);
        } else {
            $res = $this;
        }
        return $res->whereIn('status', [0, 1])->order($order)->paginate($limit);
    }

    /**
     * 获取 status 为 0，1 的数据
     * @param array $data
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getNormalListsAll($data = []) {
        $where = [];
        if (!empty($data)){
            $where = $data;
        }
        return $this->whereIn('status', [0, 1])->where($where)->select();
    }

    /**
     * 根据主键 ID 更新数据表里的数据
     * @param $id
     * @param $data
     * @return bool
     */
    public function updateById($id, $data) {
        $id = intval($id);
        if (empty($id) || empty($data) || !is_array($data)) {
            return false;
        }
        $data['update_time'] = time();
        return $this->where(['id' => $id])->save($data);
    }

    /**
     *  根据 id 获取单条数据
     * @param $id
     * @return array|bool|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getDataInfoById($id) {
        $id = intval($id);
        if (empty($id)) {
            return false;
        }
        return $this->where(['id' => $id])->find();
    }

    /**
     * 根据 ids 获取多条数据
     * @param $ids
     * @return bool|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getDataByIds($ids) {
        if (empty($ids)) {
            return false;
        }
        return $this->whereIn('id',$ids)->select();
    }


    /**
     * 根据 ids 获取数据
     * @param $ids
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getNormalInIds($ids) {
        return $this->whereIn("id", $ids)
            ->where("status", "=", config("status.mysql.table_normal"))
            ->select();
    }

    /**
     * 根据条件查询 - 二维数组
     * @param array $condition
     * @param array $order
     * @return bool|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getByCondition($condition = [], $order = ["id" => "desc"]) {
        if (!$condition || !is_array($condition)) {
            return false;
        }
        $result = $this->where($condition)->order($order)->select();
        return $result;
    }

    /**
     * 根据条件查询 - 单条数据
     * @param array $condition
     * @param array $order
     * @return bool|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getByConditionFind($condition = []) {
        if (!$condition || !is_array($condition)) {
            return false;
        }
        $result = $this->where($condition)->find();
        return $result;
    }

}